package com.tuanbiao.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;

import com.abiao.lib_utils.ActivityUtils;
import com.abiao.lib_utils.StringUtils;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.SettingController;
import com.tuanbiao.data.entity.CodeModelType;
import com.tuanbiao.databinding.ActivitySettingMoreBinding;
import com.tuanbiao.dialog.BaseDialog;
import com.tuanbiao.dialog.InputDialog;
import com.tuanbiao.dialog.MenuDialog;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.events.EncodingRulesEvent;
import com.tuanbiao.models.SettingModel;
import com.tuanbiao.presenter.setting.SettingMorePresenter;
import com.tuanbiao.ui.EncodingRulesActivity;
import com.tuanbiao.ui.account.LoginActivity;
import com.tuanbiao.utils.ClickUtil;
import com.tuanbiao.widget.RegexEditText;

import org.greenrobot.eventbus.EventBus;

public class SettingMoreActivity extends BasePresenterActivity<SettingMorePresenter, ActivitySettingMoreBinding> implements SettingController.MoreView {

    private String mMaxDetonator;
    private String mMaxDelay;
    private boolean mStandAloneModel;
    private String mIMEI;
    private String mMac;
    private int[] models = {R.string.setting_more_model_normal, R.string.setting_more_model_alone};
    private String[] allowPartNetted = {"是", "否"};
    private CodeModelType codeModelType;
    public static void start(Context context) {
        Intent starter = new Intent(context, SettingMoreActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected SettingMorePresenter createPresenter() {
        return new SettingMorePresenter(new SettingModel(), this);
    }

    @Override
    protected ActivitySettingMoreBinding getBindingView() {
        return ActivitySettingMoreBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.sbMaxDetonator.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showDetonatorDialog();
        });
        binding.sbMaxDelay.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showDelayDialog();
        });
        binding.sbModel.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showModelDialog();
        });
        binding.sbAllowPartNetted.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showAllowPartNettedDialog();
        });
        binding.sbOpenForceBlast.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showOpenForceBlastDialog();
        });
        binding.sbOpenForceAuth.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showOpenForceAuthDialog();
        });
//        binding.sbMac.setOnClickListener(view -> {
//            showMacDialog();
//        });
//        binding.sbImei.setOnClickListener(view -> {
//            showIMEIDialog();
//        });

        binding.scShowControllerParams.setOnCheckedChangeListener((compoundButton, b) -> mPresenter.setShowControlPanel(b));
        binding.scShowChangeParams.setOnCheckedChangeListener((compoundButton, b) -> mPresenter.setShowChangeModel(b));
        binding.scShowChangeServer.setOnCheckedChangeListener((compoundButton, b) -> mPresenter.setShowChangeServer(b));
        binding.scShowChangeLocal.setOnCheckedChangeListener((compoundButton, b) -> mPresenter.setShowChangeLocal(b));
        binding.scShowChangeDeviceNum.setOnCheckedChangeListener((compoundButton, b) -> mPresenter.setShowChangeDeviceNum(b));
        binding.sbEncodingRules.setOnClickListener(view -> {
            EventBus.getDefault().postSticky(new EncodingRulesEvent(true));
            EncodingRulesActivity.start(getActivity());
        });
    }

    private void showAllowPartNettedDialog() {
        // 底部选择框
        new MenuDialog.Builder(this)
                // 设置点击按钮后不关闭对话框
                //.setAutoDismiss(false)
                .setList(allowPartNetted)
                .setListener((MenuDialog.OnListener<String>) (dialog, position, string) -> {
                    mPresenter.changePartNetted(position == 0);
                })
                .setGravity(Gravity.BOTTOM)
                .setAnimStyle(BaseDialog.ANIM_BOTTOM)
                .show();
    }

    private void showOpenForceAuthDialog() {
        // 底部选择框
        new MenuDialog.Builder(this)
                // 设置点击按钮后不关闭对话框
                //.setAutoDismiss(false)
                .setList(allowPartNetted)
                .setListener((MenuDialog.OnListener<String>) (dialog, position, string) -> {
                    mPresenter.changeForceAuth(position == 0);
                })
                .setGravity(Gravity.BOTTOM)
                .setAnimStyle(BaseDialog.ANIM_BOTTOM)
                .show();
    }


    private void showOpenForceBlastDialog() {
        // 底部选择框
        new MenuDialog.Builder(this)
                // 设置点击按钮后不关闭对话框
                //.setAutoDismiss(false)
                .setList(allowPartNetted)
                .setListener((MenuDialog.OnListener<String>) (dialog, position, string) -> {
                    mPresenter.changeForceBlast(position == 0);
                })
                .setGravity(Gravity.BOTTOM)
                .setAnimStyle(BaseDialog.ANIM_BOTTOM)
                .show();
    }


    @Override
    public void showMaxDetonator(String maxDetonator) {
        mMaxDetonator = maxDetonator;
        binding.sbMaxDetonator.setRightText(maxDetonator);
    }

    @Override
    public void showMaxDelay(String maxDelay) {
        mMaxDelay = maxDelay;
        binding.sbMaxDelay.setRightText(maxDelay);
    }

    @Override
    public void showModel(boolean model) {
        binding.sbModel.setRightText(model ? R.string.setting_more_model_alone : R.string.setting_more_model_normal);
    }

    @Override
    public void showSetting(boolean showControlPanel, boolean showChangeModel, boolean showServerChange, boolean showChangeDeviceNum, boolean showLocalChange) {
        binding.scShowControllerParams.setChecked(showControlPanel);
        binding.scShowChangeParams.setChecked(showChangeModel);
        binding.scShowChangeServer.setChecked(showServerChange);
        binding.scShowChangeDeviceNum.setChecked(showChangeDeviceNum);
        binding.scShowChangeLocal.setChecked(showLocalChange);
    }

    @Override
    public void showForceBlastCheck(boolean isCheck) {
        String forceText = isCheck ?"是" : "否";
        binding.sbOpenForceBlast.getRightView().setText(forceText);
    }

    @Override
    public void showForceAuthCheck(boolean isCheck) {
        String forceText = isCheck ?"是" : "否";
        binding.sbOpenForceAuth.getRightView().setText(forceText);
    }

    @Override
    public void showAllowPartNetted(boolean isCapCountNotMatchAllowAdopt) {
        binding.sbAllowPartNetted.setRightText(isCapCountNotMatchAllowAdopt ? "是" : "否");
    }

    @Override
    public void startLogin() {
        ActivityUtils.finishAllActivities();
        LoginActivity.start(getActivity());
        finish();
    }

    private void showModelDialog() {
        // 底部选择框
        new MenuDialog.Builder(this)
                // 设置点击按钮后不关闭对话框
                //.setAutoDismiss(false)
                .setList(models)
                .setListener((MenuDialog.OnListener<String>) (dialog, position, string) -> {
                    showModelNoteDialog(position != 0);
                })
                .setGravity(Gravity.BOTTOM)
                .setAnimStyle(BaseDialog.ANIM_BOTTOM)
                .show();
    }

    private void showModelNoteDialog(boolean isStandAloneModel) {
        new MessageDialog.Builder(getActivity())
                .setTitle("提示")
                .setMessage("是否永久保留当前选择模式？")
                .setCancel("仅一次")
                .setConfirm("永久")
                .setListener(new MessageDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog) {
                        mPresenter.setModel(true, isStandAloneModel);
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        mPresenter.setModel(false, isStandAloneModel);
                    }
                }).show();
    }

    private void showDetonatorDialog() {
        new InputDialog.Builder(getActivity())
                .setTitle("设置最大组网")
                .setCanceledOnTouchOutside(false)
                .setContent(StringUtils.nullToEmpty(mMaxDetonator))
                .setInputRegex(RegexEditText.REGEX_NUMBER)
                .setInputType(InputType.TYPE_CLASS_NUMBER)
                .setListener(new InputDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog, String content) {
                        if (!TextUtils.isEmpty(content) && TextUtils.isDigitsOnly(content)) {
                            mPresenter.setMaxDetonator(content);
                            dialog.dismiss();
                        } else {
                            showMessage("请输入整数");
                        }
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();
    }

    private void showDelayDialog() {
        new InputDialog.Builder(getActivity())
                .setTitle("设置最大延时")
                .setCanceledOnTouchOutside(false)
                .setAutoDismiss(false)
                .setContent(StringUtils.nullToEmpty(mMaxDelay))
                .setInputRegex(RegexEditText.REGEX_NUMBER)
                .setInputType(InputType.TYPE_CLASS_NUMBER)
                .setListener(new InputDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog, String content) {
                        if (!TextUtils.isEmpty(content) && TextUtils.isDigitsOnly(content)) {
                            mPresenter.setMaxDelay(content);
                            dialog.dismiss();
                        } else {
                            showMessage("请输入整数");
                        }
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        dialog.dismiss();
                    }
                }).show();
    }
}